package cn.com.jcoo;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DataAuthValid implements ConstraintValidator<DataAuth, Long> {

    private DataAuthValidService dataAuthValidService;

    @Override
    public void initialize(DataAuth dataAuth) {
        ConstraintValidator.super.initialize(dataAuth);
        dataAuthValidService = DataAuthContext.getDataAuthValidService(dataAuth.validService());
    }

    @Override
    public boolean isValid(Long id, ConstraintValidatorContext constraintValidatorContext) {
        boolean hasDataAuth = dataAuthValidService.hasDataAuth(id);
        if (hasDataAuth) {
            return true;
        }
        String messageTemplate = constraintValidatorContext.getDefaultConstraintMessageTemplate();
        throw new DataAuthException(messageTemplate);
    }

}
